package cn.silver.module.authority.service.impl;

import cn.silver.framework.mybatis.service.impl.BaseServiceImpl;
import cn.silver.module.authority.domain.SysRoleDept;
import cn.silver.module.authority.mapper.SysRoleDeptMapper;
import cn.silver.module.authority.bean.RoleBean;
import cn.silver.module.authority.bean.RoleDeptBean;
import cn.silver.module.authority.service.ISysRoleDeptService;
import cn.silver.module.system.param.DeptParam;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author zhouxiaobo
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SysRoleDeptServiceImpl extends BaseServiceImpl<SysRoleDeptMapper, SysRoleDept, DeptParam, RoleDeptBean> implements ISysRoleDeptService {

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void save(RoleBean model) {
        if (CollectionUtils.isNotEmpty(model.getDeptIds())) {
            this.baseMapper.delete(new LambdaQueryWrapper<SysRoleDept>().eq(SysRoleDept::getRoleId, model.getId()));
            model.getMenuIds().stream().forEach(item -> {
                SysRoleDept cur = new SysRoleDept(model.getId(), item);
                cur.preInsert();
                this.baseMapper.insert(cur);
            });
        }
    }
}
